﻿
  ------------ lấy thông tin món ăn theo mã  ----------------------
CREATE 
--ALTER
PROC proc_LayMonTheoMa
@mama varchar(6)
AS
BEGIN
	Select * from MONAN WHERE MaMA = @mama
END
GO
  ------------ Danh Sach món ăn theo mã loại  ----------------------
exec proc_LayMonTheoMa 'MA001'

CREATE 
--ALTER
PROC proc_DanhSachMonAn
@malma varchar(6)
AS
BEGIN
	Select * from MONAN WHERE MaLoaiMA = @malma
END
GO

exec proc_DanhSachMonAn 'LM001'


-------------------  Tranh cchap dong thoi  tren mon an -------------------------
create 
--alter 
proc usp_UpdateSLMA
@mama varchar(6),@sldat int, @res int output
as
begin tran
	--set tran isolation level Serializable
    
	declare  @sl int
	set @sl = (select SLMA from MONAN  where MaMA = @mama)
	waitfor delay '0:0:05'
	begin try
		
		IF(@mama in (Select MaMA From MONAN  ))
		BEGIN
			set @sl = @sl - @sldat
			
			update MONAN 
			set SLMA = @sl
			where MaMA = @mama
			
			--select * from MONAN 
			set @res = 1
		END
		ELSE
		BEGIN
			--PRINT 'dat hang that bai'
			set @res = 0	
		END
		
	end try
	begin catch 
		
		declare @ErrorMsg varchar(2000)
		select @ErrorMsg = N'Loi: ' + ERROR_MESSAGE()
		raiserror(@ErrorMsg, 16,1)
		rollback tran
		return
		
		
	end catch
commit tran


Declare @res int
EXEC usp_UpdateSLMA 'MA001',4,@res output
print @res

select * from MONAN

